//
//  SearchSongController.m
//  Coco_Music
//
//  Created by 强哥 on 15/6/23.
//  Copyright (c) 2015年 寇广超. All rights reserved.
//

#import "SearchSongController.h"
#import "SearchSongTableCell.h"
#import "AFN_block.h"
#import "SearchSong.h"
#import "DiscoverButtonController.h"

@interface SearchSongController ()

@property(nonatomic,retain)DiscoverButtonController *ButtonController;
@property(nonatomic,assign)NSInteger page;
@property(nonatomic,assign)NSInteger Allpage;


@end

@implementation SearchSongController

-(void)dealloc
{
    [self.SingerUrl release];
    [self.SearchArray release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.SearchArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.myTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64-44) style:UITableViewStylePlain];
    self.myTable.backgroundColor = [UIColor whiteColor];
    self.myTable.dataSource = self;
    self.myTable.delegate = self;
    [self.view addSubview:self.myTable];
    [self.myTable release];
    [self SearchInfo];
    [self.myTable setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.ButtonController = [[DiscoverButtonController alloc]init];
    [self.ButtonController release];
    self.page =1;
    self.myTable.footer =[MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(SearchInfo)];
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载中,请稍后";
    [self.HUD show:YES];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _SearchArray.count;
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *cellSong = @"cellSong";
    SearchSongTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellSong];
    if (cell == nil) {
        cell = [[SearchSongTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellSong];
    }
    cell.searchSongInfo = [_SearchArray objectAtIndex:indexPath.row];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.navigationController pushViewController:self.ButtonController animated:YES];
    SearchSong *song = [_SearchArray objectAtIndexedSubscript:indexPath.row];
    self.ButtonController.SearchSpecialID = song.song_list;
    self.ButtonController.SpecialSingerPic = song.pic_url;
}

-(void)SearchInfo
{
    if (self.myTable.footer.isRefreshing ==YES && self.page <= self.Allpage) {
        self.page ++;
    }

    NSString *string = [NSString stringWithFormat:@"http://so.ard.iyyin.com/s/playlist?q=%@&page=%ld&size=15",self.SingerUrl,(long)self.page];
    NSString *url = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [AFN_block NSString:url Blokc:^(id request) {
        NSMutableArray *array =  [request objectForKey:@"data"];
        self.Allpage = [[request objectForKey:@"pages"]integerValue];
        for (NSMutableDictionary *dic in array) {
            SearchSong *search = [[SearchSong alloc]init];
            [search setValuesForKeysWithDictionary:dic];
            [self.SearchArray addObject:search];
            [search release];
        }
        [self.myTable reloadData];
        [self.myTable.footer endRefreshing];
        [self.HUD hide:YES];
    }];
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 120;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
